const Vue = require('vue');
const Common = require('./common');

let template = `<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/style/index.css"/>
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="shortcut icon" href="/favicon.ico">
    <title>答鸣服务器接口</title>
</head>
<body>
    <h3 class="title">
    <span>{{title}}</span>
      
    </h3>
    <p style="text-align:center;margin-top:15px;">  
        <a href="/static/" style="display:none;">后台管理</a>
        <a href="/interface.html">接口文档</a>
    </p>
  <div class="container">
    <ul v-if="route.path!='/'" class="box" v-for="(route,index) in routes" :key="index">
        <h3>{{route.name}}模块</h3>
        <li v-if="route.children" v-for="(item,index) in route.children" :key="index">
            <span>{{item.name}}</span> <a target="_blank" :href="route.path+item.path">{{route.path+item.path}}</a>
        </li>
    </ul>
  </div>
</body>
</html>`

const utils = require('../utils/index');

class Home extends Common {
  constructor() {
    super('Home');
  }

  async index(ctx) {
    let ip = utils.getIp();
    if (ip.match('172')) {
      ip = '132.232.87.95';
    }
    const app = new Vue({
      data: {
        ip: `http://${ip}:4000`,
        title: '答鸣的生鲜服务器接口',
        routes: [],
      },
      template,
      created() {
        this.routes = require('../router/routes');
      },
    })
    const renderer = require('vue-server-renderer').createRenderer()
    let htmlStr = await renderer.renderToString(app);
    ctx.body = htmlStr;
  }
}

module.exports = new Home();
